package com.lyun.controller;

import com.lyun.common.BaseContext;
import com.lyun.common.R;
import com.lyun.dto.CommentsDto;
import com.lyun.pojo.Comment;
import com.lyun.service.CommentLikeService;
import com.lyun.service.CommentService;
import com.lyun.vo.CommentsVo;
import com.lyun.vo.PageVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/comments")
public class CommentsController {

    @DubboReference
    private CommentService commentService;

    @DubboReference
    private CommentLikeService commentLikeService;

    /**
     * 21-APP-发布评论
     *
     * @param commentsDto
     */
    @PostMapping
    public R<String> saveComments(@RequestBody CommentsDto commentsDto) {

        Long id = BaseContext.getCurrentId();

        Comment comment = new Comment();

        //评论的各个属性赋值
        comment.setTime(LocalDateTime.now());
        comment.setUserId(id);
        comment.setComment(commentsDto.getComment());
        comment.setMovementId(commentsDto.getMovementId());

        //执行save
        commentService.save(comment);

        return R.success("评论成功");
    }

    /**
     * 22-APP-查看评论列表
     *
     * @param movementId
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping()
    public PageVO<CommentsVo> getCommentsList(@RequestParam(value = "movementId", required = false) Long movementId,
                                              @RequestParam(value = "page", required = false) Integer page,
                                              @RequestParam(value = "pagesize", required = false) Integer pagesize) {

        Long id = BaseContext.getCurrentId();

        return commentService.getCommentsVoPageByMovementId(id, movementId, page, pagesize);

    }


    /**
     * 25 app 评论点赞
     *
     * @param commentId 当前评论的id
     * @return 返回值是当前评论的点赞数
     */
    @GetMapping("/{id}/like")
    public int likeComment(@PathVariable("id") Long commentId) {

        Long userId = BaseContext.getCurrentId();

        return commentLikeService.likeComment(commentId, userId);
    }


    /**
     * 26 app 评论取消点赞
     *
     * @param commentId
     * @return 返回值是当前评论的点赞数
     */
    @GetMapping("/{id}/dislike")
    public int dislikeComment(@PathVariable("id") Long commentId) {

        Long userId = BaseContext.getCurrentId();

        return commentLikeService.dislikeComment(commentId, userId);
    }
}
